/**
 *  GTUG Zaragoza
 *  http://zaragoza.gtugs.org/
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.gtugs.zaragoza.feeds;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.gtugs.zaragoza.feeds.provider.FeedEntryContentProvider;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.os.AsyncTask;

/**
 * 
 * @author francho
 *
 */
public class LoadFeedsAsyncTask extends AsyncTask<Void,Void,Void> {

	public static String URL_FEED_NEW_MESSAGES = "http://groups.google.com/group/zaragoza-gtug/feed/atom_v1_0_msgs.xml";
	private ContentResolver mContentResolver;
	
	public LoadFeedsAsyncTask(ContentResolver contentResolver) {
		mContentResolver = contentResolver;
	}
	
	/**
	 * Tarea pesada a realizar en segundo plano 
	 * 
	 * (no tiene acceso al hilo de interface)
	 */
	@Override
	protected Void doInBackground(Void... params) {
		try {
			SAXParserFactory spf = SAXParserFactory.newInstance();
			SAXParser saxParser;
			saxParser = spf.newSAXParser();
			AtomNewMessagesHandler atomNewMessagesHandler = new AtomNewMessagesHandler();
			
			// saxParser.setProperty("http://xml.org/sax/properties/lexical-handler", atomNewMessagesHandler);

			XMLReader xmlReader = saxParser.getXMLReader();			
			xmlReader.setContentHandler(atomNewMessagesHandler);
			
			URL url = new URL(URL_FEED_NEW_MESSAGES);
			InputSource inputSource = new InputSource(url.openStream());
			inputSource.setEncoding("utf-8");
			
			xmlReader.parse(inputSource);
			
			ArrayList<ContentValues> entries = atomNewMessagesHandler.getEntries();
			
			// Cacheamos en la base de datos
			for(ContentValues entry: entries) {
				
				Cursor cursor = mContentResolver.query(
						FeedEntryContentProvider.CONTENT_URI_BASE, 
						null, 
						FeedEntry.ID + " LIKE \"" + entry.getAsString(FeedEntry.ID) + "\"",
						null,
						null
						);
				
				if(!cursor.moveToFirst()) {
					mContentResolver.insert(FeedEntryContentProvider.CONTENT_URI_BASE, entry);
				}
				cursor.close();
			
			}
			
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
		
	}

}
